package com.bs.modules.use.service.impl;

import com.bs.common.utils.DateUtils;
import com.bs.modules.app.service.UserService;
import com.bs.modules.use.entity.AppPowerEntity;
import com.bs.modules.use.entity.AppPriceEntity;
import com.bs.modules.use.entity.AppUserEntity;
import com.bs.modules.use.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.common.utils.PageUtils;
import com.bs.common.utils.Query;

import com.bs.modules.use.dao.AppComplaintDao;
import com.bs.modules.use.entity.AppComplaintEntity;
import com.bs.modules.use.service.AppComplaintService;


@Service("appComplaintService")
public class AppComplaintServiceImpl extends ServiceImpl<AppComplaintDao, AppComplaintEntity> implements AppComplaintService {

  @Autowired
  private AppUserService appUserService;

  @Autowired
  private UserService userService;

  @Autowired
  private AppComplaintDao appComplaintDao;


  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<AppComplaintEntity> page = this.page(
      new Query<AppComplaintEntity>().getPage(params),
      new QueryWrapper<AppComplaintEntity>()
    );

    for (AppComplaintEntity record : page.getRecords()) {

      AppUserEntity appUserEntity = appUserService.getById(record.getUserId());
      record.setUserName(appUserEntity.getUserUsername());
      //状态(1： 待处理 2: 已解决 3. 未解决 4. 已撤销 )
      System.out.println(record.getState());

      if (record.getState().equals("1")) {
        record.setStatusCn("待处理");
      } else if (record.getState().equals("2")) {
        record.setStatusCn("已解决");
      } else if (record.getState().equals("3")) {
        record.setStatusCn("未解决");
      } else if (record.getState().equals("4")) {
        record.setStatusCn("已撤销");
      }


    }
    return new PageUtils(page);
  }


  @Override
  public void complaint(Object openId, Object content) {
    AppComplaintEntity appComplaintEntity = new AppComplaintEntity();

    AppUserEntity appUser = userService.findAppUser(openId);

    appComplaintEntity.setUserId(appUser.getId());
    appComplaintEntity.setContent(String.valueOf(content));

    appComplaintEntity.setComplaintTime(new Date());
    appComplaintEntity.setState("1");

    this.save(appComplaintEntity);
  }

  @Override
  public List<AppComplaintEntity> getComplaint(Object openid) {
    List<AppComplaintEntity> complaint = appComplaintDao.getComplaint(String.valueOf(openid));
    for (AppComplaintEntity appComplaintEntity : complaint) {

      switch (appComplaintEntity.getState()) {
        case "1":
          appComplaintEntity.setStateCn("待处理");
          break;
        case "2":
          appComplaintEntity.setStateCn("已解决");
          break;
        case "3":
          appComplaintEntity.setStateCn("未解决");
          break;
        case "4":
          appComplaintEntity.setStateCn("已撤销");
          break;
      }

    }
    return complaint;
  }

  @Override
  public void updateState(Long id, String state) {

    String payTime = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    appComplaintDao.updateState(id, state, payTime);
  }

}
